<template>
  <el-dialog
    :model-value="visible"
    title="编辑手机号信息"
    width="500px"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" disabled />
      </el-form-item>
      
      <el-form-item label="归属地" prop="phoneAddress">
        <el-input v-model="form.phoneAddress" placeholder="请输入归属地" />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
          <el-option label="正常" :value="1" />
          <el-option label="停用" :value="0" />
        </el-select>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitLoading">
          确认修改
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { updatePhoneNumberInfo } from '@/apis/phoneNumber'

export default {
  name: 'EditPhoneNumber',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:visible', 'success'],
  setup(props, { emit }) {
    const formRef = ref()
    const submitLoading = ref(false)
    
    const form = reactive({
      phone: '',
      phoneAddress: '',
      status: 1
    })
    
    const rules = {
      phoneAddress: [
        { required: true, message: '请输入归属地', trigger: 'blur' }
      ],
      status: [
        { required: true, message: '请选择状态', trigger: 'change' }
      ]
    }
    
    // 设置表单数据
    const setFormData = (data) => {
      console.log('编辑手机号数据:', data);
      Object.keys(form).forEach(key => {
        form[key] = data[key] !== undefined ? data[key] : '';
      });
    }
    
    // 关闭弹窗
    const handleClose = () => {
      emit('update:visible', false)
    }
    
    // 提交表单
    const handleSubmit = async () => {
      try {
        await formRef.value.validate()
        submitLoading.value = true
        
        // 准备请求数据
        const requestData = {
          phone: form.phone,
          phoneAddress: form.phoneAddress,
          status: form.status
        }
        
        const res = await updatePhoneNumberInfo(requestData)
        if (res.code === 0) {
          ElMessage.success('手机号信息修改成功')
          emit('success')
          handleClose()
        } else {
          ElMessage.error(res.msg || '修改失败')
        }
      } catch (error) {
        console.error('修改手机号信息失败:', error)
        ElMessage.error('修改手机号信息失败')
      } finally {
        submitLoading.value = false
      }
    }
    
    // 暴露方法给父组件
    const exposeMethods = {
      setFormData
    }
    
    return {
      formRef,
      form,
      rules,
      submitLoading,
      setFormData,
      handleClose,
      handleSubmit,
      exposeMethods
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>